package cn.aylog.pojo;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

@Data
public class UploadFile {
    // 当前文件二进制数据
    private MultipartFile file;

    // 文件名称
    private String fileName;

    // 文件后缀
    @Setter(AccessLevel.NONE)
    @Getter(AccessLevel.NONE)
    private String extent;

    @Setter(AccessLevel.NONE)
    @Getter(AccessLevel.NONE)
    private String name;

    // 当前为第几分片
    private String currentSlice;

    // 总的分片数
    private String totalSlice;

    // 当前分片MD5值
    private String sliceMd5;

    // 原始文件MD5值
    private String originMd5;

    // 原始文件总长度
    private String length;

    public String getName() {
        int index = fileName.lastIndexOf(".");
        return fileName.substring(0, index);
    }

    public String getExtent() {
        int index = fileName.lastIndexOf(".");
        return fileName.substring(index);
    }

    public void save() {
        StringBuilder pathBuilder = new StringBuilder();
        pathBuilder.append("d:/data/temp/")
                .append(getName())
                .append("_")
                .append(this.currentSlice)
                .append(".tmp");
        String path = pathBuilder.toString();
        File temp = new File(path);
        try {
            this.file.transferTo(temp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void merge() {
        StringBuilder pathBuilder = new StringBuilder();
        pathBuilder.append("d:/data/temp/");
        String path = pathBuilder.toString();
        File tempDir = new File(path);
//        tempDir.listFiles(new FileFilter() {
//            @Override
//            public boolean accept(File pathname) {
//                boolean b = pathname.getName().startsWith(getName());
//                return b;
//            }
//        });
        // 使用 lambda
//        Object[] array = Arrays.stream(tempDir.list()).sorted(new Comparator<String>() {
//            @Override
//            public int compare(String o1, String o2) {
//                String n1 = o1.substring(o1.lastIndexOf("_") + 1, o1.lastIndexOf("."));
//                String n2 = o2.substring(o2.lastIndexOf("_") + 1, o2.lastIndexOf("."));
//                return (Integer.parseInt(n1) - Integer.parseInt(n2));
//            }
//        }).toArray();
//        for (Object name : array) {
//            System.out.println(name);
//        }

        Object[] files = Arrays.stream(tempDir.listFiles(pathname -> pathname.getName().startsWith(getName())))
                .sorted((File o1, File o2) -> {
                    String s1 = o1.getName();
                    String s2 = o2.getName();
                    String n1 = s1.substring(s1.lastIndexOf("_") + 1, s1.lastIndexOf("."));
                    String n2 = s2.substring(s2.lastIndexOf("_") + 1, s2.lastIndexOf("."));
                    return (Integer.parseInt(n1) - Integer.parseInt(n2));
                }).toArray();

        pathBuilder.append(getName()).append(getExtent());
        File dest = new File(pathBuilder.toString());
        try (FileOutputStream fos = new FileOutputStream(dest)) {
            for (Object o : files) {
                File temp = (File) o;
                System.out.println(temp.getName());
                FileInputStream fis = new FileInputStream(temp);
                byte[] data = new byte[1024];
                int len = 0;
                while ((len = fis.read(data)) != -1) {
                    fos.write(data, 0, len);
                }
                fis.close();
                temp.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        String digest = DigestUtils.md5DigestAsHex("123".getBytes());
        System.out.println(digest);
        //202cb962ac59075b964b07152d234b70
        //202cb962ac59075b964b07152d234b70
    }
}
